import axios from 'axios'
import type { Order, BaseResponse, PaginationResponse } from '../types'

// 获取订单列表
export const getOrders = (page = 1, limit = 10, params?: any): Promise<PaginationResponse<Order>> => {
  return axios.get('/api/orders', {
    params: {
      page,
      limit,
      ...params,
    },
  })
}

// 获取单个订单
export const getOrderById = (id: string): Promise<BaseResponse<Order>> => {
  return axios.get(`/api/orders/${id}`)
}

// 创建订单
export const createOrder = (orderData: Omit<Order, 'id' | 'createTime' | 'payTime'>): Promise<BaseResponse<Order>> => {
  return axios.post('/api/orders', orderData)
}

// 更新订单
export const updateOrder = (id: string, orderData: Partial<Order>): Promise<BaseResponse<Order>> => {
  return axios.put(`/api/orders/${id}`, orderData)
}

// 删除订单
export const deleteOrder = (id: string): Promise<BaseResponse<{ id: string }>> => {
  return axios.delete(`/api/orders/${id}`)
}

// 更新订单状态
export const updateOrderStatus = (id: string, status: string): Promise<BaseResponse<Order>> => {
  return axios.patch(`/api/orders/${id}/status`, {
    status,
  })
}

// 批量获取订单
export const getOrdersByIds = (ids: string[]): Promise<BaseResponse<Order[]>> => {
  return axios.get('/api/orders/batch', {
    params: {
      ids: ids.join(','),
    },
  })
}

// 获取订单统计数据
export const getOrderStats = (): Promise<BaseResponse<{ [key: string]: number }>> => {
  return axios.get('/api/orders/stats')
}